<?php
defined('IN_PHPCMS') or exit('No permission resources.');
if(!defined('CACHE_MODEL_PATH')) define('CACHE_MODEL_PATH',PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);

pc_base::load_sys_class('model', '', 0);
class picture_nsr_model extends model {
	public function __construct() {
		$this->db_config = pc_base::load_config('database');
		$this->db_setting = 'default';
		$this->table_name = 'picture_nsr';
		parent::__construct();
	}

	/**
	 * 添加redis缓存
	 */
	public function redisCache ($id,$status=0) 
	{
		$redis_cache = pc_base::load_sys_class('cache_redis','',1);
		$info = $this->get_one(array('id'=>$id));
		$key = "picture:".$id;
		if ($status == 0) {
			$redis_cache->hmset($key,$info);
		} else {
			$redis_cache->delete($key);
		}
		
	}

	/**
	 * 添加修改图片
	 */
	public function edit ($data,$id='')
	{
		if (!$id) {
			$id = $this->insert($data, true);
		} else {
			$ids = $this->update($data, array('id'=>$id));
		}
		return $id;
	}

	/**
	 * 表单验证
	 *
	 * @param array $data
	 *            表单传递的值
	 * @param string $a
	 *            add/edit添加操作时，自动加上默认值
	 */
	public function check($data, $a = 'add')
	{
		$data['title'] = strip_tags($data['title']);
		$pictureurls = $this->pictureurlsSerialize($_POST['pictureurls_url'], $_POST['pictureurls_alt']);
		$data['pictureurls'] = $pictureurls;
		$admin_model = pc_base::load_model('admin_model');
        $userInfo = $admin_model->get_one(array('userid'=>$_SESSION['userid']));
        //来源名称后台编辑用户自己手动输入
        if($data['sname_manual_flag']=='yes'){
            $data['source_name'] = $data['sname_manual']?$data['sname_manual']:'新丝路';
        }else{
            $data['source_name'] = $data['source_name']?$data['source_name']:'新丝路';
        }
        $data['source_link'] = $data['source_link']?$data['source_link']:'http://www.nsr.com.cn';
        $data['source_author'] = $data['source_author']?$data['source_author']:$userInfo['realname'];
        //删除无用的标记字段
        unset($data['sname_manual_flag']);
        unset($data['sname_manual']);
		if ($a == 'add') {
		    $rand_onclick_nums = rand(500, 1000);//浏览次数（图集入库时所随机出来的初始值）
		    $rand_collect_nums = rand(10, 50);//收藏次数（图集入库时所随机出来的初始值）
		    $rand_praise_nums = rand(50, 200);//点赞次数（图集入库时所随机出来的初始值）
			$data['siteid'] =  get_siteid();
			$data['create_time']  =date("Y-m-d H:i:s",time());
			$data['created_by'] = $_SESSION['userid'];
			$data['onclick_origin'] = $rand_onclick_nums;//浏览次数（图集入库时所随机出来的初始值）
			$data['onclick_all'] = $rand_onclick_nums;//浏览次数（接口开发时在初始值基础上每次浏览都＋1）
			$data['praise_num_origin'] = $rand_praise_nums;//点赞次数（图集入库时所随机出来的初始值）
			$data['praise_num'] = $rand_praise_nums;//点赞次数（接口开发时在初始值基础上每次浏览都＋1）
			$data['collect_num_origin'] = $rand_collect_nums;//收藏次数（图集入库时所随机出来的初始值）
			$data['collect_num'] = $rand_collect_nums;//收藏次数（接口开发时在初始值基础上每次浏览都＋1）
			$data['update_time'] =date("Y-m-d H:i:s",time());
			$data['updated_by'] = $_SESSION['userid'];
		} else {
			$data['update_time']=date("Y-m-d H:i:s",time());
			$data['updated_by'] = $_SESSION['userid'];
		}
		return $data;

	}

	/**
	 * 组图json处理
	 */
	public function pictureurlsSerialize ($url, $alt)
    {
        $data = array();
        foreach ($url as $k => $v) {
            $data[$k]['url'] = $v;
            //判断字符串中是否有中文
            if (!preg_match("/[\x7f-\xff]/", $alt[$k])) {
            	$alt[$k] = '';
			} 
            $data[$k]['alt'] = $alt[$k];
        }
        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }
}
?>